package com.atcc.test.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.atcc.test.pojo.User;
import com.atcc.test.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class UserController {

	@Autowired
	private UserService userService;
	
	@GetMapping("/test")
	public List<User> getAll() {
		List<User> result = userService.getAll();
		return result;
	}

	@GetMapping("/test/{id}")
	@HystrixCommand(fallbackMethod="hystrixGetUserById")
	public User getUserById(@PathVariable("id") Long id) {
		User result = userService.getUserById(id);
		if(result == null) {
			throw new RuntimeException();
		}
		return result;
	}
	
	public User hystrixGetUserById(@PathVariable("id") Long id) {
		User testUser = new User();
		testUser.setId(id);
		testUser.setName("用户名不存在");
		testUser.setSource("数据库不存在");
		testUser.setDepartment("部门也不存在");
		System.out.println(testUser);
		return testUser;
	}
	
}
